package org.school.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.school.dao.CaiDao;
import org.school.dao.DingdanDao;
import org.school.entity.Cai;
import org.school.entity.Dingdan;
import org.school.entity.Shop;
import org.school.dao.ShopDao;
import org.school.entity.message2;
import org.school.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import static com.xkzhangsan.time.formatter.DateTimeFormatterUtil.format;
import static com.xkzhangsan.time.formatter.DateTimeFormatterUtil.formatToDateStr;

/**
 * (Shop)表服务实现类
 *
 * @author skyme
 * @since 2023-01-13 17:44:44
 */
@Service("shopService")
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopDao shopDao;
    @Resource
    private CaiDao caiDao;
    @Resource
    private DingdanDao dingdanDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Shop queryById(Integer id) {
        return this.shopDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param shop 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Shop> queryByPage(Shop shop, PageRequest pageRequest) {
        long total = this.shopDao.count(shop);
        return new PageImpl<>(this.shopDao.queryAllByLimit(shop, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param shop 实例对象
     * @return 实例对象
     */
    @Override
    public Shop insert(Shop shop) {
        this.shopDao.insert(shop);
        return shop;
    }

    /**
     * 修改数据
     *
     * @param shop 实例对象
     * @return 实例对象
     */
    @Override
    public Shop update(Shop shop) {
        this.shopDao.update(shop);
        return this.queryById(shop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.caiDao.deleteById(id) > 0;
    }
    /**
     * 分页查询
     *
     * @param name 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Cai> getCaiByPage(String name, PageRequest pageRequest) {
        long total = this.shopDao.countByshop(name);
        return new PageImpl<>(this.shopDao.queryAllByShop(name, pageRequest), pageRequest, total);
    }

    @Override
    public List<Cai> queryCaiInfo(String name) {
        return this.shopDao.queryCaiInfo(name);
    }


    @Override
    public PageInfo<Cai> findCaiInfo(String name, int page, int size) {
        // TODO Auto-generated method stub
        PageHelper.startPage(page, size);
        List<Cai> lists = this.shopDao.queryCaiInfo(name);
        PageInfo<Cai> pageInfo = new PageInfo<Cai>(lists);
        return pageInfo;

    }

    @Override
    public int deleteByHostAndId(String host, int id) {
        //首先验证商店是否是host的，验证成功后才能进行删除,否则返回0
       Shop cshop= shopDao.queryById(id);
       if(cshop==null||!host.equals(cshop.getHost())){
           System.out.println(cshop);
           System.out.println(host+"and"+cshop.getHost());
           return 0;
       }else {
           return shopDao.deleteById(id);
       }


    }

    @Override
    public message2 completeOrder(String name, int id) {
        Dingdan dd=dingdanDao.queryById(id);
        dd.setStatus("已完成");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String date=format(new Date(),formatter);
        dd.setEndtime(date);
       int i= dingdanDao.updateByShop(dd);
       message2 mes=new message2("更新失败",0);
       if(i>0){
           mes.setStatus(1);
           mes.setContent("已完成");
       }
       return mes;
    }

    @Override
    public List<Dingdan> querryOrderByShop(String name) {
        List<Dingdan> list=dingdanDao.querryOrderByShop(name);
        for (Dingdan dingdan : list) {
            String s=redisTemplate.opsForValue().get("order:"+dingdan.getId());
            List<org.school.types.Cai> typelist= JSONObject.parseArray(s,org.school.types.Cai.class);
            dingdan.setOrder(typelist);
        }
        return list;
    }

}
